package cranim.graphics.models;

import cranim.Constants;
import cranim.graphics.Entity;

/**
 *
 * @author Benedikt
 */
public class ShotModel extends Entity implements Constants{
    
    protected UnitModel launcher;
    protected UnitModel target;
        
    protected double startX, startY, targetX, targetY, distanceX, distanceY;
        
    
    public ShotModel(UnitModel launcher, UnitModel target, String ref, double scale) {
        super(ref,launcher.getX()+launcher.getGunPosition()[X],launcher.getY()+launcher.getGunPosition()[Y], scale, launcher.getTargetAngle(target));
        this.launcher = launcher;
        this.target = target;
        startX = launcher.getX()+launcher.getGunPosition()[X];
        startY = launcher.getY()+launcher.getGunPosition()[Y];
        targetX = target.getX()+target.getSprite().getWidth()/2-this.getSprite().getWidth()/2;
        targetY = target.getY()+target.getSprite().getHeight()/2-this.getSprite().getHeight()/2;
        distanceX = targetX-startX;
        distanceY = targetY-startY;
        
    }
    
    public void move(double delta){
        this.x += (distanceX/Math.sqrt(distanceX*distanceX+distanceY*distanceY))*delta;
        this.y += (distanceY/Math.sqrt(distanceX*distanceX+distanceY*distanceY))*delta;        
    }
    
    public boolean hasHit(){
        return ((Math.abs(getMiddle()[X]-targetX) < SHOT_ERROR) && (Math.abs(getMiddle()[Y]-targetY) < SHOT_ERROR));
    }
}
